/*lib.js*/
/*create sub of obj and set type or id*/
function __createNode(obj,type,id)
{
    var subObj = document.createElement(type);
    if(id) subObj.id = id;
    obj.appendChild(subObj);
    return subObj;
}
function __removeClass(obj,name)
{
    var tmp = Function("return /"+name+"/;");
    var str = obj.className.replace(tmp(),'');
    obj.className = str.trim();

}
function __setAtt(obj,name,value)
{
    obj.setAttribute(name,value);
}
function __listClass(obj,style)
{
    var l = style.length;
    if(l <= 0) return;
    var css  = obj.className;
    for(var x=0;x<l;x++)  css += ' '+style[x];
    obj.setAttribute("class", css.trim());
}
function __pushClass(obj,style)
{
    nameList = obj.className + ' ' + style;
    obj.setAttribute("class", nameList.trim());
}

function __LoopFindId(obj)
{
    var upObj = obj.parentNode;
    if(upObj.id) return upObj.id;
    else return __LoopFindId(upObj);
}

function addListener(element,act)
{
    var clk = __call('EA_click_'+act);
    var over= __call('EA_over_'+act);
    var down = __call('EA_down_'+act);
    var out = __call('EA_out_'+act);
    var up = __call('EA_up_'+act);
    var move = __call('EA_move_'+act);
    var scroll = __call('EA_scroll_'+act);
    var focus = __call('EA_focus_'+act);
    var change = __call('EA_change_'+act);
    if(clk) __Listener(element,"click",clk);
    if(over) __Listener(element,"mouseover",over);
    if(down) __Listener(element,"mousedown", down);
    if(out) __Listener(element,"mouseout", out);
    if(up) __Listener(element,"mouseup",up);
    if(move) __Listener(element,"mousemove",move);
    if(scroll)  __Listener(element,"scroll",scroll);
    if(focus) __Listener(element,'focus',focus);
    if(change) __Listener(element,'change',change);
}
function enterDown(e)
{
    var f = getActive().id;
    var func = __call('EA_enter_'+f);
    if(func) func(e);
}
function getActive()
{
    return document.activeElement;
}
function keyDown(e)
{
    var em = IE ? event : arguments[0];
    var act = getActive();
    switch(em.keyCode)
    {
        case 13:
            if(act.className == 'pt_editer')
            {
                editerEnter(act);
            }
            enterDown(e);
        break;
        case 8:
            if(act.className == 'pt_editer')
            {
                editerBackspaceObj(e);
            }     
        break;
        case 116:
       
        break;
        default:
        break;
    } 
}
function editerEnter(act)
{
    var range = ptSelection();
    var now = range.anchorNode;
    var next = now.nextSibling;
    var p =createParagraph('<br />');
/*    if(next)
    {
        range.selectNode(next);
        range.insertNode(p);
        return;
    }*/
}

function editerBackspaceObj(e)
{
    var range = ptSelection();
    var textObj = range.anchorNode;
    var node = textObj.parentNode;
    if(node.tagName == 'div') return;
    if(node.innerText.length == 1)
    {
        var divObj = node.parentNode;
        if(divObj.firstChild == node)
        {
            node.innerHTML = '<br />';
            node.focus();   
            return;
        }
        prevNode = node.previousSibling;
        range = ptRange();
        range.selectNode(prevNode);
        if(prevNode.nodeType >1)
        {
            //prevNode.focus();
        }
        divObj.removeChild(node);
    }
}
function frameDoc(obj)
{
    var f = IE ? obj.document : obj.contentDocument;
    return f;
}

var cNode = function(obj, supers)
{
    if(isObj(obj) == false) return;
    var isBtn = false;
    if(obj.type == 'pt_button')
    {
        var btn = createBtn(obj.text);
        obj.text = false;
        obj.type = "div";
        var isBtn = true;
    }
    var subObj = __createNode(supers,obj.type, obj.id);
    if(isBtn) subObj.appendChild(btn);
    if(obj.style && obj.style.length>0)
    {
         __listClass(subObj,obj.style);
    }
    if(obj.text)
    {
        subObj.innerHTML = obj.text;
    }
    var att = obj.attribute;
    if(att)
    {
        for(a in att)
        {
            __setAtt(subObj,a,att[a]);
        }
    }
    if(obj.event_act){
        var a = obj.event_act;
    }else if(obj.id){
        var a = obj.id;
    }else{
        var a =  __LoopFindId(subObj);
        
    }
    addListener(subObj,a);
    return subObj;
}
var cElement = function (obj,supers)
{
    var newObj = cNode(obj,supers);
    if(obj.sub)
    {
        for(var i in obj.sub)
        {
             cElement(obj.sub[i], newObj);
        }
    }
    return newObj;
}
function findNext(n)
{
    if(n > 0) return PF[n-1];
    else
    return PF[n+1];    
}
function getObjPos(obj)
{
    var y = obj.offsetTop;
    var x = obj.offsetLeft;
    var height = obj.offsetHeight;
    while(obj = obj.offsetParent) {    
        x += obj.offsetLeft;    
        y += obj.offsetTop;    
    }
    return {'x':x,'y':y,'h':height};
}
function getMousePos(e)
{
    if(IE) e = event;else e = arguments[0];
    
    return {"x":e.clientX,"y":e.clientY};
}
function chgTitleBtn(e,t)
{
    var obj = __E(e);
    if(t == 'r')
    {
        __removeClass(obj,'pt_talk_box_btn');
    }else{
        __pushClass(obj,'pt_talk_box_btn');
    }
}
function ptRange()
{
    if(IE) return document.selection.createRange();
    return document.createRange();
}
function ptSelection()
{
    if(IE) return ptRange().text; 
    else return window.getSelection();
}
function ptCaret()
{
    if(IE) return document.selection.createRange().duplicate(); 
}
function ptInsert(node,type,args)
{
    if(IE)
    {
    }else{
        var Obj = document.createElement(type);
        try{
            for(arb in args){
                if(arb == 'text')
                {
                    Obj.innerHTML = args.text;
                    continue;
                }
                Obj.setAttribute(arb,args[arb]);
            }
        }catch(e){}
        node.insertNode(Obj);
    }
}
function ptExecCom()
{
    var args = arguments;
    var code = args[0];
    var mod = args[1] ? args[1] : false;
    var param = args[2] ? args[2] : null;
    if(IE) return document.selection.createRange().execCommand(code);
    else document.execCommand(code, mod , param);
}
function scroll(obj){
       var self = this;
       this.obj = obj;
       this.content = this.obj.getElementsByTagName('div')[0];
       this.bar = document.createElement('div');
       this.bar.className = 'pt_scroll_bar';
       this.bar.style.marginTop = 0;
       this.bar.style.height = parseInt( this.obj.offsetHeight / this.content.scrollHeight  * this.obj.offsetHeight) + 'px';
       
       this.obj.appendChild( this.bar );
       this.bar.y;
       this.srcElement;
       this.marginTop;
       this.bar.onmousedown = function(e){ self.mousedown(e); }
}
scroll.prototype = {
    mousedown:function(e){           
        var self = this;
        var e = e || window.event;
        self.bar.y = e.clientY;
        self.bar.t = parseInt( self.bar.style.marginTop );
        document.onmousemove = function(e){ self.mousemove(e); }
        stopDefault(e);
    },
    mousemove:function(e){
        var e = e || window.event;
        this.marginTop = this.bar.t + ( e.clientY - this.bar.y );
        if(this.marginTop < 0 ) {
            this.marginTop = 0;
        }
        if( this.marginTop > this.obj.clientHeight - this.bar.offsetHeight ){
                this.marginTop = this.obj.clientHeight - this.bar.offsetHeight;
            }
            this.bar.style.marginTop = this.marginTop + 'px';
            this.content.scrollTop = ( this.content.scrollHeight - this.obj.offsetHeight ) * parseInt( this.marginTop ) / ( this.obj.clientHeight - this.bar.clientHeight );
            document.onmouseup = function(e){ document.onmousemove = null; }
            stopDefault(e);
        }
}
function stopDefault( e ) {
    if ( e && e.preventDefault )
    e.preventDefault();
    else
        window.event.returnValue = false;
    return false;
}
function loadImg(imgObj)
{
    if(IMG == false) imgObj.src="./images/u.png";
    else IMG = false;
}
function createLoadImg(url)
{
    var img = document.createElement('img');
    img.src = url;
    return img;
} 
function PTPing()
{
    A.getCall('ping',ping);
    //setTimeout(PTPing,PINGTIME);
}
function updateMsg()
{
    A.getCall('update/msg',showMsg);
}
function getBlog()
{
    A.getCall('update/blog',getBLog);
}
function updateRss()
{
    A.getCall('update/rss',showRss);
}
function updateFellow()
{
    A.getCall('update/fellow',updateFellowData);
}
function ping(data)
{
    if(data)
    {
        var d = Function("return "+data+";");
        var action = d().p;
        switch(action)
        {
            case "msg"://the talk msg update
            updateMsg();
            break;
            case "blog"://the blog update
            getBlog()
            break;
            case "rss"://the user RSS update
            updateRss();
            break;
            case "fellow"://the user online status update
            updateFellow();
            break;
            default:
    //        alert(data);
            break;
        }
    }
    return;
}
function sendReplyStatus(reText)
{
    if(reText)
    {
        var d = Function("return "+reText+";");
        var re = d();
        //var my = Function("return "+reText+";");
        //alert(my);
        //var re = my();
        if(re.u) alert('success'+re.data);
        else alert('error');
    }
}
function findBtnNode(obj)
{
    if(obj.className == "pt_btn_base")
        return obj.parentNode;
    this.max = 3;
    this.index =1;
    var re = findclass(this,"pt_btn_base",obj);
    return re.parentNode;
}
function findEditerNode(obj)
{
    if(obj.contentEditable == true)
        return obj;
    this.max = 6;
    this.index = 1;
    var re = findclass(this,"pt_editer",obj);
    if(re.contentEditable == true)
        return obj;
    return false;
}
function findclass(f,classname,obj)
{
    f.index +=1;
    if(f,index > f.max) return false;
    buff = obj.parentNode;
    if(buff.className == classname)
        return buff
    return findclass(f,classname,buff)
}
function setFocusBox(id)
{
    if(id == '') return;
    if(NOWTALKBOX){
        try{
            $(NOWTALKBOX).removeClass('pt_talk_box_up');  
        }catch(e){}
    }
    $(id).pushClass('pt_talk_box_up');
    NOWTALKBOX = id;
}
function isChild(p,c)
{
    if(p == c) return true; 
    var child = p.childNodes;
    var len = child.length;
    for(var i=0;i<len;i++)
    {
        var next = child[i];
        if(next == c)
        {
            return true;
        }else{
            if(next.childNodes.length > 0)
            {
                if(isChild(next,c))
                {
                    return true;
                }
            }
        }
    }
    return false;
}
function loopCNodeData(level,nodeType,loopdata,tag)
{
    var re_arr = [];
    var style = '';
    for(var i=0;i<level;i++)
    {
        var colors = "#"+loopdata[i];
        var cssname = "pt_color_panel_"+tag+i;
        style += "."+cssname+"{border-color:"+colors+";background-color:"+colors+"}\n";
        var re_arr_sub = {"type":nodeType,"sub":[{"type":"div","style":[cssname],"event_act":"get_color_value","attribute":{"title":colors}}]};
        re_arr.append(re_arr_sub);
    }
    createCSS(style);
    return re_arr;
}
function createCSS(style)
{
    var obj= document.createElement('style');
    var head = document.getElementsByTagName('head')[0];
    obj.setAttribute("type", "text/css");
    if(IE)
    {
        obj.styleSheet.cssText = style;
    }else if(FF){
        obj.innerHTML = style;
    }else{
        obj.appendChild(document.createTextNode(style));
    }
    head.appendChild(obj);
}
function opDisplay(element,act)
{
    if($(element))
    {
        var style = $(element).style.display;
        if(style == "none")
        {
            $(element).style.display = "block";
            EA(act,true);
            return "block";
        }else{
            $(element).style.display = "none";
            EA(null,false);
            return "none";
        }
    }else{
        return true;
    }
}
